<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        var num = prompt('请输入一个数n,求1-n的和:');
        document.write('1-'+ num +'的和是：' + sum(num));


        function sum(num){
            let ans = 0;
            for (let i = 1; i <= num; i++){
                ans += i;
            }
            return ans;
        }

    </script>
</body>
</html>